import styles from '../styles.less';
import {App, Button} from 'antd';
import {ExclamationCircleFilled} from '@ant-design/icons';
import {connect, useAccess, Access} from 'umi';

const Opera = (props) => {

  const {modal} = App.useApp();
  const {selectedRowKeys, setSelectedRowKeys, tablePayload, setTablePayload} = props.data;
  const {dispatch, submitting} = props;
  const access = useAccess();

  function deleteAll(e) {
    e.preventDefault();
    if (!submitting) {
      modal.confirm({
        title: '提示',
        icon: <ExclamationCircleFilled/>,
        content: '是否确定清空数据？',
        okText: "确认",
        cancelText: "取消",
        onOk() {
          dispatch({
            type: 'PSystemLog/delAll',
            callback: () => {
              setTablePayload({...tablePayload, page: 1});
              setSelectedRowKeys([]);
            },
          });
        },
      });
    }
  }

  /* 删除数据 */
  function del(e) {
    e.preventDefault();
    if (!submitting) {
      modal.confirm({
        title: '提示',
        icon: <ExclamationCircleFilled/>,
        content: '是否确定删除数据？',
        okText: "确认",
        cancelText: "取消",
        onOk() {
          dispatch({
            type: 'PSystemLog/del',
            payload: selectedRowKeys,
            callback: () => {
              setTablePayload({...tablePayload, page: 1});
              setSelectedRowKeys([]);
            },
          });
        },
      });
    }
  }

  return (
    <div className={styles.myOperaAction}>
      <div className={styles.leftContent}>
        <Access accessible={access?.system_log_delAll ?? false}>
          <Button type='primary' onClick={deleteAll}>
            清空
          </Button>
        </Access>
        <Access accessible={access?.system_log_del ?? false}>
          <Button type='primary' danger onClick={del} disabled={selectedRowKeys.length <= 0}>
            删除
          </Button>
        </Access>
        <span style={{marginLeft: 8}}>{`已选 ${selectedRowKeys.length} 项`}</span>
      </div>
    </div>
  );
};

export default connect(({loading}) => {
  return {
    submitting: loading.effects['PSystemLog/del'] || loading.effects['PSystemLog/delAll'],
  };
})(Opera);
